<?php
namespace Android\Controller;

class TeachplanController extends BaseController {


    /**
     * 获取指定教师的教学计划
     *
     * @param integer $tid      教师ID
     * @param integer $term_id  学期ID
     * @return string json      返回所有教学计划列表
     */
    public function get() {
        $teacher_id = I ('request.id','');
        $term_id = $this->term['id'];

        if (empty($teacher_id))
            $this->out($this->fail,'Empty id');

        $result = D ('Teachplan')->getTeachplanArr($teacher_id,$term_id);
        if (empty($result))
            $this->out($this->fail,'当前没有教学计划');

        // 获取部分学生信息
        $Student = D ('Student');
        $Timetable = D ('Timetable');
        foreach ($result as $key=>$val) {
            $result[$key]['weeks'] = $Timetable->getWeeksByTid($val['id']);
            $result[$key]['student'] = $Student->getStudentByIdsStr($val['student_ids']);
        }
        $this->out($this->success,'success',$result);
    }


    /**
     * 获取教学计划下得课表信息
     *
     * @param integer $teachplan_id     教学计划ID
     * @param integer $week             周次
     * @return string json
     */
    public function getTimetable () {

        $teachplan_id = I ('request.id','');
        $week = I ('request.week',getTimeInfo($this->term['stime'],$this->term['etime'],true));

        if (empty($teachplan_id))
            $this->out($this->fail,'Empty id');
        if ($week === false)
            $this->out($this->fail,'当前不是教学时间');

        $timetables = D ('Timetable')->getTimetable($teachplan_id, $week);
        empty($timetables) && $timetables = null;

        $this->out($this->success, 'Success', $timetables);
    }


    /**
     * 获取当前需要考情的教学计划
     *
     * @param integer $id    教师ID
     * @return string json
     */
    public function getAttendTeachplan() {

        $teacher_id = I ('request.id','');
        $term_id = $this->term['id'];

        if (empty($teacher_id))
            $this->out($this->fail,'Empty id');

        // 获取当前上课时间信息
        $timeInfo = getTimeInfo($this->term['stime'],$this->term['etime']);
        if ($timeInfo === false) {
            $this->out($this->fail,'当前不是教学时间');
        }

        $Teachplan = D ('Teachplan');
        $ids = $Teachplan->getTeachplanIdsByTid($teacher_id,$term_id);
        if (empty($ids))
            $this->out($this->fail,'nothing');

        $idsStr = implode(',',i_array_column($ids,'id'));

        // 构造查询条件
        $map['week'] = $timeInfo['week'];
        $map['xq'] = $timeInfo['xq'];
        $map['teachplan_id'] = array('in',$idsStr);
        $map['start'] = array('elt',$timeInfo['jc']);
        $map['end'] = array('egt',$timeInfo['jc']);


        $Timetable = D ('Timetable');
        $result = $Timetable->getTimetableByWhere($map);
        if (count($result) >= 2)
            $this->out($this->fail,'教学计划课程安排有冲突');

        // 获取当前教学计划的详细信息
        $teachplan_id = $result[0]['teachplan_id'];
        $teachplan = $Teachplan->get($teachplan_id);
        if (empty($teachplan))
            $this->out($this->fail,'当前没有教学计划需要考勤');

        // 计算上课总人数
        $sids = explode(',',$teachplan['student_ids']);
        $teachplan['student_count'] = count($sids);
        $teachplan['course'] = $result[0];
        $this->out($this->success,'Success',$teachplan);
    }


    /**
     * 获取指定教学计划下面的学生
     */
    public function getStudents ()
    {

        $teachplan_id = I('request.id', '');
        if (empty($teachplan_id))
            $this->out($this->fail, 'Empty id');

        $Teachplan = D('Teachplan');
        $idsArr = $Teachplan->getStudentIds($teachplan_id);

        if (empty($idsArr))
            $this->out($this->fail, '没有学生');

        $Student = D('Student');
        $Holiday = D('Holiday');
        $studentArr = array();
        foreach ($idsArr as $val) {
            $s = $Student->where('id = ' . $val)->getField('id,student_id,name,sex,telphone,photo');
            // 获取请假信息
            $holiday = $Holiday->getHolidayBySid($val);
            $stime = strtotime($holiday['stime']);
            $etime = strtotime($holiday['etime']);
            $now = time();
            if ($now < $etime && $now > $stime) {
                $s[$val]['holiday'] = $holiday;
            }
            if (!empty($s)) $studentArr[] = $s[$val];
        }

        if (empty($studentArr))
            $this->out($this->fail, '没有学生');

        $this->out($this->success, 'Success', $studentArr);
    }

}